<?php

namespace app\supplier\model\user;

use app\common\model\user\User as UserModel;
/**
 * 收藏模型
 */
class User extends UserModel
{
	/**
     * 获取用户统计数量
     */
    public function getUserData($cityid = 0, $startDate, $endDate, $type, $refereelist=[])
    {
        $model = $this;

        if (!is_null($startDate)) {
            $model = $model->where('create_time', '>=', strtotime($startDate));
        }
        if (is_null($endDate)) {
            $model = $model->where('create_time', '<', strtotime($startDate) + 86400);
        } else {
            $model = $model->where('create_time', '<', strtotime($endDate) + 86400);
        }

        if ($type == 'user_total' || $type == 'user_add') {
            return $model->where(function ($query) use ($cityid,$refereelist){
                    $query->where(function ($query) use ($cityid){
                            $query->where('referee_id','=',0)->where('city_id', '=', $cityid);
                        })
                        ->whereOr(function ($query) use ($refereelist){
                            $query->where('referee_id','in',$refereelist);
                        });
                })->count();
        } else if ($type == 'user_pay') {
            return $model->where(function ($query) use ($cityid,$refereelist){
                    $query->where(function ($query) use ($cityid){
                        $query->where('referee_id','=',0)->where('city_id', '=', $cityid);
                    })
                        ->whereOr(function ($query) use ($refereelist){
                            $query->where('referee_id','in',$refereelist);
                        });
                })->where('pay_money', '>', '0')->count();
        } else if ($type == 'user_no_pay') {
            return $model->where(function ($query) use ($cityid,$refereelist){
                    $query->where(function ($query) use ($cityid){
                        $query->where('referee_id','=',0)->where('city_id', '=', $cityid);
                    })
                        ->whereOr(function ($query) use ($refereelist){
                            $query->where('referee_id','in',$refereelist);
                        });
                })->where('pay_money', '=', '0')->count();
        } elseif($type == 'referee'){
            return $model->where('referee_id','in',$refereelist)->count();
        }
        return 0;
    }

    /**
     * 获取用户基本信息
     */
//    public function getUserList($mobile='',$params)
//    {
//        if($mobile==''){
//            return [];
//        }
//        $model = $this;
//        $model = $model->where('mobile', '=', $mobile);
//        $model = $model->where('is_delete', '=', 0);
//        return $model->field("user_id,avatarUrl,nickName,mobile,create_time")->paginate($params);
//    }

    /**
     *获取用户基本信息
     * getUserList
     * @param $mobile
     * @param $params
     * mixed
     * static
     * @return mixed
     * @Author kevin
     * @date 2024/5/23 17:47
     */
    public static function getUserList($mobile='',$params)
    {
        $model = new static();

        $nickName = $params['nickName']??'';
        $grade_id = $params['grade_id']??'';
        //检索：用户名
        if (!empty($nickName)) {
            $model = $model->where('user.nickName|user.mobile|user.user_id', 'like', '%' . $nickName . '%');
        }

        if($mobile){
            $model = $model->where('mobile', '=', $mobile);
        }


        $where = [];


        if (isset($params['province_id']) && $params['province_id'] >0) {
            $where[] = ['user.province_id','=',$params['province_id']];
        }

        if (isset($params['city_id']) && $params['city_id'] >0) {
            $where[] = ['user.city_id','=',$params['city_id']];
        }


        if (isset($params['nick_name']) && $params['nick_name']) {
            $where[] = ['user.nickName|user.mobile|user.user_id','=',$params['nick_name']];
        }

        if (isset($params['is_golden']) && $params['is_golden'] >0) {
            $where[] = ['user.is_golden','=',$params['is_golden']];
        }



        // 获取用户列表
        return $model->alias('user')->distinct(true)->field(['user.*'])
            ->where('user.is_delete', '=', '0')
            ->where($where)
            ->order(['user.create_time' => 'desc'])
            ->hidden(['open_id', 'union_id'])
            ->paginate($params);
    }

    public static function getRefereeLists($params)
    {
        $model = new self;
        $model = $model->where('is_delete', '=', '0');
        if($params['god_supplier_id']){
            $model = $model->where('god_supplier_id', 'like', '%"' .$params['god_supplier_id']. '"%');
        }
        return $model->column('user_id');
    }
}